def cleanup_configuration():
  TenantName = GetOption("TenantName")
  SwitchName1 = "PythonSwitch1"
  SwitchName2 = "PythonSwitch2"
  SwitchName3 = "PythonSwitch3"
  AgentLogins11 = []
  for i in range(0,3):
    AgentLogins11.append(str(1100+i))
  AgentLogins12 = []
  for i in range(0,3):
    AgentLogins12.append(str(2100+i))  
  AgentLogins21 = []
  for i in range(0,3):
    AgentLogins21.append(str(2200+i))   
  AgentLogins23 = []
  for i in range(0,2):
    AgentLogins23.append(str(2300+i))       
  AgentLogins31 = []
  for i in range(0,2):
    AgentLogins31.append(str(3100+i))         
  Persons1 = []
  for i in range(0,3):
    Persons1.append("P_1_2_" + str(100+i))    
  Persons2 = []
  for i in range(0,3):
    Persons2.append("P_2_" + str(200+i))    
  Persons3 = []
  for i in range(0,2):
    Persons3.append("P_2_3" + str(300+i))       
    
  Skills1 = []
  for i in range(0,3):
    for j in range(1,3):
      Skills1.append("Skill" + str(i)+ str(j))    
  #--------------------------------------
  CfgAgentLogins11 = []  
  CfgAgentLogins12 = []  
  CfgAgentLogins21 = []  
  CfgAgentLogins23 = []  
  
  CfgAgentLogins31 = []  
  
  CfgSkills1 = []  
  CfgPersons1 = []  
  CfgPersons2 = []  
  CfgPersons3 = []  
  
  CfgSwitch1 = None
  CfgSwitch2 = None
  CfgSwitch3 = None
  
  #--------------------------------------
  CfgTenant1 = CfgTenant(TenantName)
  #--------------------------------------
  PrintOn(0)
  try:
    CfgSwitch1 = CfgSwitch(TenantName, SwitchName1)
    CfgSwitch1.Delete()
  except:
    pass    
  try:
    CfgSwitch2 = CfgSwitch(TenantName, SwitchName2)
    CfgSwitch2.Delete()
  except:
    pass    
  try:
    CfgSwitch3 = CfgSwitch(TenantName, SwitchName3)
    CfgSwitch3.Delete()
  except:
    pass        
  try:
    CfgPhysicalSwitch1 = CfgPhysicalSwitch("G3_NEW")
    CfgPhysicalSwitch1.Delete()
  except:
    pass
  for skill in Skills1:
    try:
      cfgSkill = CfgSkill(TenantName, skill)    
      cfgSkill.Delete()
    except:
      pass
    
  for person in Persons1:
    try:
      cfgPerson = CfgPerson(TenantName, person)    
      cfgPerson.Delete()
    except:
      pass   
      
  for person in Persons2:
    try:
      cfgPerson = CfgPerson(TenantName, person)    
      cfgPerson.Delete()
    except:
      pass   
  for person in Persons3:
    try:
      cfgPerson = CfgPerson(TenantName, person)    
      cfgPerson.Delete()
    except:
      pass        
  PrintOn(1)
  TestHandler().SetTestResult(None)  
  ResetFatalErrorCnt()

def testWMF_CONFIG_1():
  """Changing Agent Site assignment, WMF_CONFIG semi manual test"""
  
  WFM = model.AbstractServer()
  WFM.appType = "WFM"
  raw_input("Make sure WFM Config is not running")
  dbInfo = CfgDAP.userProperties["default"]
  oldDBname = dbInfo["dbname"] 
  print oldDBname
  if not GetOption("DBName"):
    ProgrammError("Option DBName must be specified")  
  try:

    CfgDAP.BeginChange()
    CfgDAP.userProperties["default"]["dbname"] = GetOption("DBName")
    CfgDAP.EndChange()  
    dbInfo = CfgDAP.userProperties["default"]
    ScsWFMDataAggregator.Stop()  
    #--------------------------------------
    WMOnlyOneLogin = 1  
    WMUnassignAgents = 1
    #--------------------------------------
    TenantName = GetOption("TenantName")
    SwitchName1 = "PythonSwitch1"
    SwitchName2 = "PythonSwitch2"
    SwitchName3 = "PythonSwitch3"
    
    AgentLogins11 = []
    for i in range(0,3):
      AgentLogins11.append(str(1100+i))
    AgentLogins12 = []
    for i in range(0,3):
      AgentLogins12.append(str(2100+i))  
    AgentLogins21 = []
    for i in range(0,3):
      AgentLogins21.append(str(2200+i))      
      
    AgentLogins23 = []
    for i in range(0,2):
      AgentLogins23.append(str(2300+i))       
    AgentLogins31 = []
    for i in range(0,2):
      AgentLogins31.append(str(3100+i)) 
      
    Persons1 = []
    for i in range(0,3):
      Persons1.append("P_1_2_" + str(100+i))    
    Persons2 = []
    for i in range(0,3):
      Persons2.append("P_2_" + str(200+i))     
    Persons3 = []
    for i in range(0,2):
      Persons3.append("P_2_3" + str(300+i))       
      
    Skills1 = []
    for i in range(0,3):
      for j in range(1,3):
        Skills1.append("Skill" + str(i)+ str(j))    
    #--------------------------------------
    CfgAgentLogins11 = []  
    CfgAgentLogins12 = []  
    CfgAgentLogins21 = []  
    CfgAgentLogins23 = []  
    CfgAgentLogins31 = []
    
    CfgSkills1 = []  
    CfgPersons1 = []  
    CfgPersons2 = []  
    CfgPersons3 = []  
    
    CfgSwitch1 = None
    CfgSwitch2 = None
    CfgSwitch3 = None
    
    #--------------------------------------
    CfgTenant1 = CfgTenant(TenantName)
    #--------------------------------------

    try:

      CfgPhysicalSwitch1 = CfgPhysicalSwitch()
      CfgPhysicalSwitch1.SetAttributes({"name": "G3_NEW", "type":CfgSwitchType.CFGLucentDefinityG3})
      CfgPhysicalSwitch1.Add()

      CfgSwitch1 = CfgSwitch()
      CfgSwitch1.SetAttributes({"name": SwitchName1, "physSwitchDBID":CfgPhysicalSwitch1 .DBID, "tenantDBID":CfgTenant1.DBID})
      CfgSwitch1.Add()

      CfgSwitch2 = CfgSwitch()
      CfgSwitch2.SetAttributes({"name": SwitchName2, "physSwitchDBID":CfgPhysicalSwitch1 .DBID, "tenantDBID":CfgTenant1.DBID})
      CfgSwitch2.Add()  
      PrintLog("Clear WM_EXCEPTION_TYPES, WM_SITES, WM_AGENTS, WM_SKILLS, WM_AGENT_SKILLS")

      cmd = "delete from WM_EXCEPTION_TYPES"
      results = DBServer.ExecSQL(cmd, dbInfo)

      cmd = "delete from WM_SITES"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_AGENTS"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_SKILLS"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_AGENT_SKILLS" 
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_TIMEZONES" 
      results = DBServer.ExecSQL(cmd, dbInfo)    
      cmd ="delete from WM_GLOBAL_SETTINGS" 
      results = DBServer.ExecSQL(cmd, dbInfo)    
      
      cfgServer = CfgSwitch1.cfgServer
      fltr = {"tenant_dbid": 1, "name": "PST"} # read PST from Environment 
      strTimeZone = cfgServer.GetObjectInfo(CfgObjectType.CFGTimeZone, fltr)[0]
      timeZoneDBID = cfgServer.GetObjectDBIDFromString(strTimeZone)
      timeZoneOffset = (float(cfgServer.GetObjectIntPropertyFromString(strTimeZone, "offset"))/2)*60
      cmd = "insert into WM_TIMEZONES (WM_NAME, WM_VALUE, GSW_TIMEZONE_ID, WM_ALLOW_SYNCH) values ('PST', %d, %d, 1)" %(timeZoneOffset, timeZoneDBID)
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd = "select WM_TIMEZONE_ID from WM_TIMEZONES where GSW_TIMEZONE_ID = %d" %timeZoneDBID
      results = DBServer.ExecSQL(cmd, dbInfo)
      tzID = int(results[0]['WM_TIMEZONE_ID'])
      PrintLog("Insert sites %s, %s into WM_SITES" %(CfgSwitch1.name, CfgSwitch2.name))
      cmd = "insert into WM_SITES (GSW_SWITCH_ID, WM_NAME, GSW_NAME, WM_TIMEZONE_ID, \
                                  WM_UNASSIGN_AGENTS, WM_ONLY_ONE_LOGIN) \
                                  values (%d, '%s', '%s', %d, %d, %d)" \
                                  %(CfgSwitch1.DBID, CfgSwitch1.name, CfgSwitch1.name, \
                                  tzID, WMUnassignAgents, WMOnlyOneLogin)
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd = "insert into WM_SITES (GSW_SWITCH_ID, WM_NAME, GSW_NAME, WM_TIMEZONE_ID, \
                                  WM_UNASSIGN_AGENTS, WM_ONLY_ONE_LOGIN) \
                                  values (%d, '%s', '%s', %d, %d, %d)" \
                                  %(CfgSwitch2.DBID, CfgSwitch2.name, CfgSwitch2.name, \
                                  tzID, WMUnassignAgents, WMOnlyOneLogin)
      results = DBServer.ExecSQL(cmd, dbInfo)  

      cmd = "insert into WM_SITES (WM_NAME,  WM_TIMEZONE_ID, \
                                  WM_UNASSIGN_AGENTS, WM_ONLY_ONE_LOGIN) \
                                  values ('%s', %d, %d, %d)" \
                                  %( "WM_CRAZY_SITE", tzID, WMUnassignAgents, \
                                  WMOnlyOneLogin)
      results = DBServer.ExecSQL(cmd, dbInfo)  

      cmd = "insert into WM_GLOBAL_SETTINGS (WM_ID, WM_EMAIL_SYNCH_ENABLED, WM_SCH_AUDIT_LEVEL, WM_CAL_AUDIT_LEVEL) values (1, 1, 0, 0)"
      results = DBServer.ExecSQL(cmd, dbInfo)  


      cmd = "select WM_SITE_ID from WM_SITES where GSW_SWITCH_ID = %d" %CfgSwitch1.DBID
      results = DBServer.ExecSQL(cmd, dbInfo)
      siteID1 = int(results[0]['WM_SITE_ID'])
      cmd = "select WM_SITE_ID from WM_SITES where GSW_SWITCH_ID = %d" %CfgSwitch2.DBID
      results = DBServer.ExecSQL(cmd, dbInfo)
      siteID2 = int(results[0]['WM_SITE_ID'])  
      PrintLog("Site ID for %s = %d, for %s = %d " %(CfgSwitch1.name, siteID1, CfgSwitch2.name, siteID2))

      PrintLog("Add %d Agent Logins for %s"%(len(AgentLogins11), CfgSwitch1.name))
      for loginCode in AgentLogins11:
        cfgAgentLogin = CfgAgentLogin()
        cfgAgentLogin.SetAttributes({"loginCode": loginCode, "tenantDBID":CfgTenant1.DBID, "switchDBID":CfgSwitch1.DBID})
        cfgAgentLogin.Add()  
        CfgAgentLogins11.append(cfgAgentLogin)

      PrintLog("Add %d Agent Logins for %s"%(len(AgentLogins12),CfgSwitch2.name))
      for loginCode in AgentLogins12:
        cfgAgentLogin = CfgAgentLogin()
        cfgAgentLogin.SetAttributes({"loginCode": loginCode, "tenantDBID":CfgTenant1.DBID, "switchDBID":CfgSwitch2.DBID})
        cfgAgentLogin.Add()  
        CfgAgentLogins12.append(cfgAgentLogin)   

      PrintLog("Add %d Agent Logins for %s"%(len(AgentLogins21),CfgSwitch2.name))
      for loginCode in AgentLogins21:
        cfgAgentLogin = CfgAgentLogin()
        cfgAgentLogin.SetAttributes({"loginCode": loginCode, "tenantDBID":CfgTenant1.DBID, "switchDBID":CfgSwitch2.DBID})
        cfgAgentLogin.Add()  
        CfgAgentLogins21.append(cfgAgentLogin)        

      PrintLog("Add %d skills"%len(Skills1))
      for skill in Skills1:
        cfgSkill = CfgSkill()
        cfgSkill.SetAttributes({"name": skill, "tenantDBID":CfgTenant1.DBID})
        cfgSkill.Add()
        CfgSkills1.append(cfgSkill)



      ind1 = 0  
      PrintLog("Add %d Agents with 1 login to %s and 1 login to %s and 2 skills"%(len(Persons1), CfgSwitch1.name, CfgSwitch2.name))
      PrintLog("These agents should created as unassigned in WMF")
      for userName in Persons1:
        cfgPerson = CfgPerson()
        agentLoginInfo1 = CfgAgentLoginInfo()
        agentLoginInfo1.agentLoginDBID = CfgAgentLogins11[ind1/2].DBID
        agentLoginInfo2 = CfgAgentLoginInfo()
        agentLoginInfo2.agentLoginDBID = CfgAgentLogins12[ind1/2].DBID
        skillLevel1 = CfgSkillLevel()
        skillLevel1.skillDBID = CfgSkills1[ind1].DBID
        skillLevel2 = CfgSkillLevel()
        skillLevel2.skillDBID = CfgSkills1[ind1+1].DBID 
        agentInfo = CfgAgentInfo()
        agentInfo.agentLogins = [agentLoginInfo1, agentLoginInfo2]
        agentInfo.skillLevels = [skillLevel1, skillLevel2]
        cfgPerson .SetAttributes({"agentInfo": agentInfo, "employeeID": userName, \
                                  "lastName": "LN" + userName, "firstName": "FN" + userName,\
                                  "tenantDBID":CfgTenant1.DBID, "userName": userName, \
                                  "emailAddress": userName + "@justmail.com"})

        cfgPerson.Add()
        CfgPersons1.append(cfgPerson)
        ind1 = ind1 + 2  

      ind1 = 0  
      PrintLog("Add %d Agents with 1 login to %s and 2 skills"%(len(Persons2), CfgSwitch2.name))
      PrintLog("These agents should created as assigned to %s site in WMF" %CfgSwitch2.name)
      for userName in Persons2:
        cfgPerson = CfgPerson()
        agentLoginInfo2 = CfgAgentLoginInfo()
        agentLoginInfo2.agentLoginDBID = CfgAgentLogins21[(ind1)/2].DBID
        skillLevel1 = CfgSkillLevel()
        skillLevel1.skillDBID = CfgSkills1[ind1].DBID
        skillLevel2 = CfgSkillLevel()
        skillLevel2.skillDBID = CfgSkills1[ind1+1].DBID 
        agentInfo = CfgAgentInfo()
        agentInfo.agentLogins = [agentLoginInfo2]
        agentInfo.skillLevels = [skillLevel1, skillLevel2]
        cfgPerson .SetAttributes({"agentInfo": agentInfo, "employeeID": userName, "lastName": \
                                  "LN" + userName, "firstName": "FN" + userName,\
                                  "tenantDBID":CfgTenant1.DBID, "userName": userName, \
                                  "emailAddress": userName + "@justmail.com"})
        cfgPerson.Add()
        CfgPersons2.append(cfgPerson)
        ind1 = ind1 + 2  


      raw_input("Make SYNCHRONIZATION on WFM_Config. Press Enter to continue")

      cmd = "select WM_EMAIL_SYNCH_ENABLED from WM_GLOBAL_SETTINGS"
      results = DBServer.ExecSQL(cmd, dbInfo)
      emailSync = int(results[0]["WM_EMAIL_SYNCH_ENABLED"])  


      PrintLog("Checking tables after synchronization")
      for cfgSkill in CfgSkills1:
        cmd = "select * from WM_SKILLS where GSW_SKILL_ID = %d and WM_NAME = '%s'" %(cfgSkill.DBID, cfgSkill.name)
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "New skill added, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_SKILLS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))    

      for cfgPerson in CfgPersons1:
        #unassigned agents should be added
        if emailSync and cfgPerson.emailAddress:
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID is NULL \
                and GSW_EMAIL = '%s' \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName, cfgPerson.emailAddress)
        else:               
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID is NULL \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName)

        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))
        else:  
          cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d and  GSW_SKILL_ID = %d and GSW_SKILL_LEVEL = %d" \
                 %(cfgPerson.DBID, cfgPerson.agentInfo.skillLevels[0].skillDBID, cfgPerson.agentInfo.skillLevels[0].level ) 
          time.sleep(1)
          results = DBServer.ExecSQL(cmd, dbInfo)
          if len(results) != 1:
            WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)  
            cmd = "select * from WM_AGENT_SKILLS"
            PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))

          cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d and  GSW_SKILL_ID = %d and GSW_SKILL_LEVEL = %d" \
                 %(cfgPerson.DBID, cfgPerson.agentInfo.skillLevels[1].skillDBID, cfgPerson.agentInfo.skillLevels[1].level ) 
          time.sleep(1)
          results = DBServer.ExecSQL(cmd, dbInfo)
          if len(results) != 1:
            WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)
            cmd = "select * from WM_AGENT_SKILLS"
            PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))




      for cfgPerson in CfgPersons2:  
        #assigned agents should be added
        if emailSync and cfgPerson.emailAddress:

          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d \
                and GSW_EMAIL = '%s' \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName,  siteID2, cfgPerson.emailAddress)
        else:
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName,  siteID2)

        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))
        else:
          cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d and  GSW_SKILL_ID = %d and GSW_SKILL_LEVEL = %d" \
                 %(cfgPerson.DBID, cfgPerson.agentInfo.skillLevels[0].skillDBID, cfgPerson.agentInfo.skillLevels[0].level ) 
          time.sleep(1)
          results = DBServer.ExecSQL(cmd, dbInfo)
          if len(results) != 1:
            WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)  
            cmd = "select * from WM_AGENT_SKILLS"
            PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))

          cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d and  GSW_SKILL_ID = %d and GSW_SKILL_LEVEL = %d" \
                 %(cfgPerson.DBID, cfgPerson.agentInfo.skillLevels[1].skillDBID, cfgPerson.agentInfo.skillLevels[1].level ) 
          time.sleep(1)
          results = DBServer.ExecSQL(cmd, dbInfo)
          if len(results) != 1:
            WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)
            cmd = "select * from WM_AGENT_SKILLS"
            PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))



      #-----------------------------------------------      
      #Delete agent logins from CfgAgentLogins12
      PrintLog("Delete Agent Logins for Site2 belonging to persons with 2 agent logins. Persons should be assigned to Site1")
      for cfgAgentLogin in CfgAgentLogins12:
        cfgAgentLogin.Delete()
        #agents should be assigned to site1  
      raw_input("Make SYNCHRONIZATION on WFM_Config. Press Enter to continue")  

      for cfgPerson in CfgPersons1:
        cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and WM_SITE_ID = %d" %(cfgPerson.DBID, siteID1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "Agent should be assigned to site, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS where GSW_AGENT_ID = %d" % cfgPerson.DBID
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      


      #-----------------------------------------------      
      #Delete agent logins from CfgAgentLogins11
      PrintLog("Delete Agent Logins for Site1. Persons from Site1 should go unassigned")
      ind = 0
      for cfgAgentLogin in CfgAgentLogins11:
        cfgAgentLogin.Delete()

      raw_input("Make SYNCHRONIZATION on WFM_Config. Press Enter to continue")  
      for cfgAgentLogin in CfgAgentLogins11:
        cfgPerson = CfgPersons1[ind]
        #Agents form site1 should go unissigned
        cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and WM_SITE_ID is NULL"%cfgPerson.DBID
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "Agent login, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS where GSW_AGENT_ID = %d "%cfgPerson.DBID
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      
        ind = ind + 1
      #-----------------------------------------------
      #Delete person
      PrintLog("Delete Persons assigned to Site2. WM_END_DATE should be set to current")
      for cfgPerson in CfgPersons2:
        cfgPerson.Delete()

      raw_input("Make SYNCHRONIZATION on WFM_Config. Press Enter to continue")  
      for cfgPerson in CfgPersons2:
        cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and WM_END_DATE = '%s'" \
               %(cfgPerson.DBID, time.strftime("%Y-%m-%d 00:00:00.000", time.localtime()))             
  #             %(cfgPerson.DBID, time.strftime("%Y-%m-%d 00:00:00.000", time.gmtime()))
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "Person deleted, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS where GSW_AGENT_ID = %d"%cfgPerson.DBID
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      
      raw_input("############")
      CfgSwitch3 = CfgSwitch()
      CfgSwitch3.SetAttributes({"name": SwitchName3, "physSwitchDBID":CfgPhysicalSwitch1.DBID, "tenantDBID":CfgTenant1.DBID})
      CfgSwitch3.Add()  

      PrintLog("Add %d Agent Logins for %s"%(len(AgentLogins23),CfgSwitch2.name))
      for loginCode in AgentLogins23:
        cfgAgentLogin = CfgAgentLogin()
        cfgAgentLogin.SetAttributes({"loginCode": loginCode, "tenantDBID":CfgTenant1.DBID, "switchDBID":CfgSwitch2.DBID})
        cfgAgentLogin.Add()  
        CfgAgentLogins23.append(cfgAgentLogin)  
        
      PrintLog("Add %d Agent Logins for %s"%(len(AgentLogins31),CfgSwitch3.name))
      for loginCode in AgentLogins31:
        cfgAgentLogin = CfgAgentLogin()
        cfgAgentLogin.SetAttributes({"loginCode": loginCode, "tenantDBID":CfgTenant1.DBID, "switchDBID":CfgSwitch3.DBID})
        cfgAgentLogin.Add()  
        CfgAgentLogins31.append(cfgAgentLogin)  
        
      PrintLog("Add 1 Agent with 1 login to %s and 1 login to %s "%(CfgSwitch2.name, CfgSwitch3.name))
      userName = Persons3[0]
      cfgPerson = CfgPerson()
      agentLoginInfo1 = CfgAgentLoginInfo()
      agentLoginInfo1.agentLoginDBID = CfgAgentLogins23[0].DBID
      agentLoginInfo2 = CfgAgentLoginInfo()
      agentLoginInfo2.agentLoginDBID = CfgAgentLogins31[0].DBID
      agentInfo = CfgAgentInfo()
      agentInfo.agentLogins = [agentLoginInfo1, agentLoginInfo2]
      cfgPerson .SetAttributes({"agentInfo": agentInfo, "employeeID": userName, \
                                "lastName": "LN" + userName, "firstName": "FN" + userName,\
                                "tenantDBID":CfgTenant1.DBID, "userName": userName, \
                                "emailAddress": userName + "@justmail.com"})

      cfgPerson.Add()
      CfgPersons3.append(cfgPerson)

      raw_input("Make SYNCHRONIZATION on WFM_Config. DO NOT ADD SITE %s. Press Enter to continue" %CfgSwitch3.name)  
        
      userName = Persons3[0]
      cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and WM_SITE_ID = %d" %(cfgPerson.DBID, siteID2)
      results = DBServer.ExecSQL(cmd, dbInfo)
      if len(results) != 1:
        WFM.testValue(description = "Agent should be assigned to site, wrong results for %s:\n %s" %(cmd, results), error = 1)
        cmd = "select * from WM_AGENTS where GSW_AGENT_ID = %d" % cfgPerson.DBID
        PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      



      cmd = "insert into WM_SITES (GSW_SWITCH_ID, WM_NAME, GSW_NAME, WM_TIMEZONE_ID, \
                                  WM_UNASSIGN_AGENTS, WM_ONLY_ONE_LOGIN) \
                                  values (%d, '%s', '%s', %d, %d, %d)" \
                                  %(CfgSwitch3.DBID, CfgSwitch3.name, CfgSwitch3.name, \
                                  tzID, WMUnassignAgents, WMOnlyOneLogin)

      results = DBServer.ExecSQL(cmd, dbInfo)  


      cmd = "select WM_SITE_ID from WM_SITES where GSW_SWITCH_ID = %d" %CfgSwitch3.DBID
      results = DBServer.ExecSQL(cmd, dbInfo)
      siteID3 = int(results[0]['WM_SITE_ID'])
      
      PrintLog("Add 1 Agent with 1 login to %s and 1 login to %s "%( CfgSwitch2.name, CfgSwitch3.name))
      userName = Persons3[1]
      cfgPerson = CfgPerson()
      agentLoginInfo1 = CfgAgentLoginInfo()
      agentLoginInfo1.agentLoginDBID = CfgAgentLogins23[1].DBID
      agentLoginInfo2 = CfgAgentLoginInfo()
      agentLoginInfo2.agentLoginDBID = CfgAgentLogins31[1].DBID
      agentInfo = CfgAgentInfo()
      agentInfo.agentLogins = [agentLoginInfo1, agentLoginInfo2]
      cfgPerson .SetAttributes({"agentInfo": agentInfo, "employeeID": userName, \
                                "lastName": "LN" + userName, "firstName": "FN" + userName,\
                                "tenantDBID":CfgTenant1.DBID, "userName": userName, \
                                "emailAddress": userName + "@justmail.com"})

      cfgPerson.Add()
      CfgPersons3.append(cfgPerson)

      raw_input("Make SYNCHRONIZATION on WFM_Config. Press Enter to continue")  
        
      userName = Persons3[1]
      cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and WM_SITE_ID is NULL" %cfgPerson.DBID
      results = DBServer.ExecSQL(cmd, dbInfo)
      if len(results) != 1:
        WFM.testValue(description = "Agent should be unasigned, wrong results for %s:\n %s" %(cmd, results), error = 1)
        cmd = "select * from WM_AGENTS where GSW_AGENT_ID = %d" % cfgPerson.DBID
        PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))             
      
      raw_input("end")
    finally:
      for obj in CfgPersons1:    
        if obj.exists:
          obj.Delete()

      for obj in CfgPersons2:    
        if obj.exists:
          obj.Delete()      
      for obj in CfgPersons3:    
        if obj.exists:
          obj.Delete()
      for obj in CfgSkills1:    
        if obj.exists:
          obj.Delete()      

      for obj in CfgAgentLogins11:    
        if obj.exists:
          obj.Delete() 

      for obj in CfgAgentLogins12:    
        if obj.exists:
          obj.Delete()  
      for obj in CfgAgentLogins21:    
        if obj.exists:
          obj.Delete()  
      for obj in CfgAgentLogins23:    
        if obj.exists:
          obj.Delete()  
      for obj in CfgAgentLogins31:    
        if obj.exists:
          obj.Delete()  
      if CfgSwitch1 and CfgSwitch1.exists:
        CfgSwitch1.Delete()
      if CfgSwitch2 and CfgSwitch2.exists:
        CfgSwitch2.Delete()
      if CfgSwitch3 and CfgSwitch3.exists:
        CfgSwitch3.Delete()

      if CfgPhysicalSwitch1.exists:  
        CfgPhysicalSwitch1.Delete()
      #-----------------------------------------------
      
  finally:
    CfgDAP.BeginChange()
    CfgDAP.userProperties["default"]["dbname"] = oldDBname
    CfgDAP.EndChange()  


      
def testtWMF_CONFIG_2():
  WFM = model.AbstractServer()
  WFM.appType = "WFM"
  raw_input("Make sure WFM Config is not running")
  dbInfo = CfgDAP.userProperties["default"]
  oldDBname = dbInfo["dbname"]  
  if not GetOption("DBName"):
    ProgrammError("Option DBName must be specified")  
  try:

    CfgDAP.BeginChange()
    CfgDAP.userProperties["default"]["dbname"] = GetOption("DBName")
    CfgDAP.EndChange()  
    dbInfo = CfgDAP.userProperties["default"]
    ScsWFMDataAggregator.Stop()  

    #--------------------------------------
    WMOnlyOneLogin = 1  
    WMUnassignAgents = 1
    #--------------------------------------
    TenantName = GetOption("TenantName")
    SwitchName1 = "PythonSwitch1"
    AgentLogins1 = []
    for i in range(0,3):
      AgentLogins1.append(str(1100+i))
    Persons1 = []
    for i in range(0,3):
      Persons1.append("P_1_2_" + str(100+i))    
    Skills1 = []
    for i in range(0,3):
      for j in range(1,3):
        Skills1.append("Skill" + str(i)+ str(j))    
    #--------------------------------------
    CfgAgentLogins1 = []  
    CfgSkills1 = []  
    CfgPersons1 = []  
    CfgSwitch1 = None
    #--------------------------------------
    CfgTenant1 = CfgTenant(TenantName)
    try:

      CfgPhysicalSwitch1 = CfgPhysicalSwitch()
      CfgPhysicalSwitch1.SetAttributes({"name": "G3_NEW", "type":CfgSwitchType.CFGLucentDefinityG3})
      CfgPhysicalSwitch1.Add()

      CfgSwitch1 = CfgSwitch()
      CfgSwitch1.SetAttributes({"name": SwitchName1, "physSwitchDBID":CfgPhysicalSwitch1 .DBID, "tenantDBID":CfgTenant1.DBID})
      CfgSwitch1.Add()

      PrintLog("Clear WM_SITES, WM_AGENTS, WM_SKILLS, WM_AGENT_SKILLS")
      cmd = "delete from WM_SITES"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_AGENTS"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_SKILLS"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_AGENT_SKILLS" 
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_TIMEZONES" 
      results = DBServer.ExecSQL(cmd, dbInfo)   
      cmd ="delete from WM_GLOBAL_SETTINGS" 
      results = DBServer.ExecSQL(cmd, dbInfo) 
      cfgServer = CfgSwitch1.cfgServer
      fltr = {"tenant_dbid": 1, "name": "PST"} # read PST from Environment 
      strTimeZone = cfgServer.GetObjectInfo(CfgObjectType.CFGTimeZone, fltr)[0]
      timeZoneDBID = cfgServer.GetObjectDBIDFromString(strTimeZone)
      timeZoneOffset = (float(cfgServer.GetObjectIntPropertyFromString(strTimeZone, "offset"))/2)*60
      cmd = "insert into WM_TIMEZONES (WM_NAME, WM_VALUE, GSW_TIMEZONE_ID, WM_ALLOW_SYNCH) values ('PST', %d, %d, 1)" %(timeZoneOffset, timeZoneDBID)
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd = "select WM_TIMEZONE_ID from WM_TIMEZONES where GSW_TIMEZONE_ID = %d" %timeZoneDBID
      results = DBServer.ExecSQL(cmd, dbInfo)
      tzID = int(results[0]['WM_TIMEZONE_ID'])
      PrintLog("Insert site %s, into WM_SITES" %(CfgSwitch1.name))
      cmd = "insert into WM_SITES (GSW_SWITCH_ID, WM_NAME, GSW_NAME, WM_TIMEZONE_ID, \
                                  WM_UNASSIGN_AGENTS, WM_ONLY_ONE_LOGIN) \
                                  values (%d, '%s', '%s', %d, %d, %d)" \
                                  %(CfgSwitch1.DBID, CfgSwitch1.name, CfgSwitch1.name, \
                                  tzID, WMUnassignAgents, WMOnlyOneLogin)
      results = DBServer.ExecSQL(cmd, dbInfo)

      cmd = "insert into WM_SITES (WM_NAME,  WM_TIMEZONE_ID, \
                                  WM_UNASSIGN_AGENTS, WM_ONLY_ONE_LOGIN) \
                                  values ('%s', %d, %d, %d)" \
                                  %( "WM_CRAZY_SITE", tzID, WMUnassignAgents, \
                                  WMOnlyOneLogin)
      results = DBServer.ExecSQL(cmd, dbInfo)  

      cmd = "insert into WM_GLOBAL_SETTINGS (WM_ID, WM_EMAIL_SYNCH_ENABLED, WM_SCH_AUDIT_LEVEL, WM_CAL_AUDIT_LEVEL) values (1, 1, 0, 0)"
      results = DBServer.ExecSQL(cmd, dbInfo)  


      cmd = "select WM_SITE_ID from WM_SITES where GSW_SWITCH_ID = %d" %CfgSwitch1.DBID
      results = DBServer.ExecSQL(cmd, dbInfo)
      siteID1 = int(results[0]['WM_SITE_ID'])
      PrintLog("Site ID for %s = %d" %(CfgSwitch1.name, siteID1))

      PrintLog("Add %d Agent Logins for %s"%(len(AgentLogins1), CfgSwitch1.name))
      for loginCode in AgentLogins1:
        cfgAgentLogin = CfgAgentLogin()
        cfgAgentLogin.SetAttributes({"loginCode": loginCode, "tenantDBID":CfgTenant1.DBID, "switchDBID":CfgSwitch1.DBID})
        cfgAgentLogin.Add()  
        CfgAgentLogins1.append(cfgAgentLogin)


      PrintLog("Add %d skills"%len(Skills1))
      for skill in Skills1:
        cfgSkill = CfgSkill()
        cfgSkill.SetAttributes({"name": skill, "tenantDBID":CfgTenant1.DBID})
        cfgSkill.Add()
        CfgSkills1.append(cfgSkill)



      ind1 = 0  
      PrintLog("Add %d Agents with 1 login to %s and 2 skills"%(len(Persons1), CfgSwitch1.name))
      PrintLog("These agents should created as assigned to Site1 WMF")
      for userName in Persons1:
        cfgPerson = CfgPerson()
        agentLoginInfo1 = CfgAgentLoginInfo()
        agentLoginInfo1.agentLoginDBID = CfgAgentLogins1[ind1/2].DBID

        skillLevel1 = CfgSkillLevel()
        skillLevel1.skillDBID = CfgSkills1[ind1].DBID
        skillLevel2 = CfgSkillLevel()
        skillLevel2.skillDBID = CfgSkills1[ind1+1].DBID 
        agentInfo = CfgAgentInfo()
        agentInfo.agentLogins = [agentLoginInfo1]
        agentInfo.skillLevels = [skillLevel1, skillLevel2]
        cfgPerson .SetAttributes({"agentInfo": agentInfo, "employeeID": userName, \
                                  "lastName": "LN" + userName, "firstName": "FN" + userName,\
                                  "tenantDBID":CfgTenant1.DBID, "userName": userName})
        cfgPerson.Add()
        CfgPersons1.append(cfgPerson)
        ind1 = ind1 + 2  

      ind1 = 0  

      raw_input("Make SYNCHRONIZATION on WFM_Config. Press Enter to continue")
      cmd = "select WM_EMAIL_SYNCH_ENABLED from WM_GLOBAL_SETTINGS"
      results = DBServer.ExecSQL(cmd, dbInfo)
      emailSync = int(results[0]["WM_EMAIL_SYNCH_ENABLED"])  

      PrintLog("Checking tables after synchronization")
      for cfgSkill in CfgSkills1:
        cmd = "select * from WM_SKILLS where GSW_SKILL_ID = %d and WM_NAME = '%s'" %(cfgSkill.DBID, cfgSkill.name)
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "New skill added, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_SKILLS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))    



      for cfgPerson in CfgPersons1:  
        #assigned agents should be added

        if emailSync and cfgPerson.emailAddress:
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d \
                and GSW_EMAIL = '%s' \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName, siteID1, cfgPerson.emailAddress)
        else:               
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName, siteID1)


        #cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
        #      and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d and WM_END_DATE is NULL" \
        #       %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName,  siteID1)
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))
        else:
          cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d and  GSW_SKILL_ID = %d and GSW_SKILL_LEVEL = %d" \
                 %(cfgPerson.DBID, cfgPerson.agentInfo.skillLevels[0].skillDBID, cfgPerson.agentInfo.skillLevels[0].level ) 
          time.sleep(1)
          results = DBServer.ExecSQL(cmd, dbInfo)
          if len(results) != 1:
            WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)  
            cmd = "select * from WM_AGENT_SKILLS"
            PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))

          cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d and  GSW_SKILL_ID = %d and GSW_SKILL_LEVEL = %d" \
                 %(cfgPerson.DBID, cfgPerson.agentInfo.skillLevels[1].skillDBID, cfgPerson.agentInfo.skillLevels[1].level ) 
          time.sleep(1)
          results = DBServer.ExecSQL(cmd, dbInfo)
          if len(results) != 1:
            WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)
            cmd = "select * from WM_AGENT_SKILLS"
            PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))


      #-----------------------------------------------
      #Change Switch Name
      PrintLog("Change switch name from %s to PythonSwitch1NameChanged" %CfgSwitch1.name )
      CfgSwitch1.BeginChange()
      newName = "PythonSwitch1NameChanged"
      CfgSwitch1.name = newName
      CfgSwitch1.EndChange()
      raw_input("Make SYNCHRONIZATION on WFM_Config. Press Enter to continue")
      cmd = "select * from WM_SITES where GSW_SWITCH_ID = %d and GSW_NAME = '%s'" %(CfgSwitch1.DBID, newName)
      time.sleep(1)
      results = DBServer.ExecSQL(cmd, dbInfo)
      if len(results) != 1:
        WFM.testValue(description = "Switch name changed, wrong results for %s:\n %s" %(cmd, results), error = 1)     
        cmd = "select * from WM_SITES where GSW_SWITCH_ID = %d"%CfgSwitch1.DBID
        PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))

      #-----------------------------------------------      


      for cfgPerson in CfgPersons1:
        cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and WM_SITE_ID = %d" %(cfgPerson.DBID, siteID1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "Agent should be assigned to site, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS where GSW_AGENT_ID = %d" % cfgPerson.DBID
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      


      #-----------------------------------------------
      #Delete one Skill from agent
      PrintLog("Delete one Skill shortcut from persons" )    
      for cfgPerson in CfgPersons1:
        cfgPerson.BeginChange()
        removedSkillDBID = cfgPerson.agentInfo.skillLevels[0].skillDBID
        del cfgPerson.agentInfo.skillLevels[0]
        cfgPerson.EndChange()

      raw_input("Make SYNCHRONIZATION on WFM_Config. Press Enter to continue")
      for cfgPerson in CfgPersons1:
        cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d and GSW_SKILL_ID = %d"  %(cfgPerson.DBID, removedSkillDBID)  
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 0:
          WFM.testValue(description = "Skill removed from person, wrong results for %s:\n %s" %(cmd, results), error = 1)     
          cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d" %cfgPerson.DBID
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      

      #-----------------------------------------------
      #Change skill name
      PrintLog("Cbange Skill names" )    
      for cfgSkill in CfgSkills1:
        cfgSkill.BeginChange()
        cfgSkill.name = cfgSkill.name + "Changed"
        cfgSkill.EndChange()
      raw_input("Make SYNCHRONIZATION on WFM_Config. Press Enter to continue")
      for cfgSkill in CfgSkills1:      
        cmd = "select * from WM_SKILLS where GSW_SKILL_ID = %d and WM_NAME = '%s'" %(cfgSkill.DBID, cfgSkill.name)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "Skill name changed, wrong results for %s:\n %s" %(cmd, results), error = 1)     
          cmd = "select * from WM_SKILLS where GSW_SKILL_ID = %d" %cfgSkill.DBID
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      

      #-----------------------------------------------
      #Delete the rest of skills 
      PrintLog("Delete  the rest of Skills" )    
      for cfgSkill in CfgSkills1:
        cfgSkill.Delete()
      raw_input("Make SYNCHRONIZATION on WFM_Config. On sckill delettion select YES. Press Enter to continue")
      for cfgSkill in CfgSkills1:      
        cmd = "select * from WM_SKILLS where GSW_SKILL_ID = %d" %cfgSkill.DBID
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 0:
          WFM.testValue(description = "Skill deleted, wrong results for %s:\n %s" %(cmd, results), error = 1)     
          cmd = "select * from WM_SKILLS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      

        cmd = "select * from WM_AGENT_SKILLS where GSW_SKILL_ID = %d"  %cfgSkill.DBID 
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 0:
          WFM.testValue(description = "Skill deleted, wrong results for %s:\n %s" %(cmd, results), error = 1)     
          cmd = "select * from WM_SKILLS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      


      #-----------------------------------------------
      #Change persons lastname, first name
      PrintLog("Change  persons lastname, first name" )    
      for cfgPerson in CfgPersons1:
        cfgPerson.BeginChange()
        cfgPerson.lastName = cfgPerson.lastName + "Changed"
        cfgPerson.firstName = cfgPerson.firstName + "Changed"
        cfgPerson.EndChange()
      raw_input("Make SYNCHRONIZATION on WFM_Config. Press Enter to continue")
      for cfgPerson in CfgPersons1:  
        if emailSync and cfgPerson.emailAddress:
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d \
                and GSW_EMAIL = '%s' \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName, siteID1, cfgPerson.emailAddress)
        else:               
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName, siteID1)



        #cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
        #      and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d and WM_END_DATE is NULL" \
        #      %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName,  siteID1)

        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "Person changed, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS where GSW_AGENT_ID = %d"%cfgPerson.DBID
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      


      #-----------------------------------------------
      #Last Delete Switch
      PrintLog("Delete Switch related to Site1. GSW_NAME and GSW_SWITCH_ID should be set to NULL")
      CfgSwitch1.Delete()
      raw_input("Make SYNCHRONIZATION on WFM_Config. Press Enter to continue")
      cmd = "select * from WM_SITES where WM_SITE_ID = %d and GSW_NAME = '' and GSW_SWITCH_ID is NULL" %siteID1
      time.sleep(1)
      results = DBServer.ExecSQL(cmd, dbInfo)
      if len(results) != 1:
        WFM.testValue(description = "Switch deleted, wrong results for %s:\n %s" %(cmd, results), error = 1)       
        cmd = "select * from WM_SITES"
        PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      

    finally:
      for obj in CfgPersons1:    
        if obj.exists:
          obj.Delete()  


      for obj in CfgSkills1:    
        if obj.exists:
          obj.Delete()      

      for obj in CfgAgentLogins1:    
        if obj.exists:
          obj.Delete() 


      if CfgSwitch1 and CfgSwitch1.exists:
        CfgSwitch1.Delete()


      if CfgPhysicalSwitch1.exists:  
        CfgPhysicalSwitch1.Delete()   

      #-----------------------------------------------
  finally:
    CfgDAP.BeginChange()
    CfgDAP.userProperties["default"]["dbname"] = oldDBname
    CfgDAP.EndChange()   

def testWMF_CONFIG_3():
  """Testing security"""
  raw_input("Make sure WFM Config is not running")
  dbInfo = CfgDAP.userProperties["default"]
  oldDBname = dbInfo["dbname"]  
  if not GetOption("DBName"):
    ProgrammError("Option DBName must be specified")  
  try:

    CfgDAP.BeginChange()
    CfgDAP.userProperties["default"]["dbname"] = GetOption("DBName")
    CfgDAP.EndChange()  
    dbInfo = CfgDAP.userProperties["default"]
    ScsWFMDataAggregator.Stop() 
    
    TenantName = GetOptionMandatory("TenantName")
    CfgTenant1 = CfgTenant(TenantName)
    
    SwitchName1 = "PythonSwitch1"

    Persons1 = []
    for i in range(0,3):
      Persons1.append("P_1_2_" + str(100+i))  

    Skills1 = []
    for i in range(0,3):
      for j in range(1,3):
        Skills1.append("Skill" + str(i)+ str(j)) 
        
    CfgPersons1 = []
    CfgSkills1 = []
    
    #additional cleanup
    PrintLog("Adding/retrieving CfgPerson admin01")
        
    PrintOn(0)
    
    try:

      cfgAdmin = CfgPerson(TenantName, "admin01")
      cfgAdmin.Delete()
    except:
      print "admin01 does not exist"

    PrintOn(1)
    TestHandler().SetTestResult(None)  
    ResetFatalErrorCnt()  
    #end cleanup
    
    
    

    


    try:
      PrintLog("Clear WM_SITES, WM_AGENTS, WM_SKILLS, WM_AGENT_SKILLS")
      cmd = "delete from WM_SITES"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_AGENTS"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_SKILLS"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_AGENT_SKILLS" 
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_TIMEZONES" 
      results = DBServer.ExecSQL(cmd, dbInfo)  
      cmd ="delete from WM_GLOBAL_SETTINGS" 
    
      userName = "admin01"
      cfgAdmin = CfgPerson()
      cfgAdmin .SetAttributes({ "employeeID": userName, \
                                "lastName":  userName, "firstName":  userName,\
                                "tenantDBID":CfgTenant1.DBID, "userName": userName, "isAgent": CfgFlag.False.val })
      cfgAdmin.Add()    
      cfgAdminID = cfgAdmin.cfgID
      folderConfiguration = CfgFolder("Configuration")
      folderConfiguration.AddACE(cfgAdminID, "PEDXHCR") # all

      cmd = "insert into WM_USERS (GSW_USER_ID, WM_TIMEZONE_ID, WM_USER, WM_BUILDER_ID, \
            WM_SECURITY_ROLE_ID, GSW_FIRST_NAME, GSW_LAST_NAME, GSW_EMAIL) \
            values (%d, NULL, '%s', NULL, NULL, '%s', '%s', '%s')" \
            %(cfgAdmin.DBID, cfgAdmin.userName, cfgAdmin.firstName, cfgAdmin.lastName, cfgAdmin.emailAddress)
      results = DBServer.ExecSQL(cmd, dbInfo)

      cmd = "select WM_SUBSYSTEM_ID from WM_SUBSYSTEMS"
      results = DBServer.ExecSQL(cmd, dbInfo)  
      print results
      for each in results:
        subsystemID = each['WM_SUBSYSTEM_ID']
        cmd = "insert into WM_USER_SUBSYSTEMS (GSW_USER_ID, WM_SUBSYSTEM_ID) values (%d, %d)" \
              %(cfgAdmin.DBID, int(subsystemID))
        DBServer.ExecSQL(cmd, dbInfo)      

      PrintLog("Adding new objects. Setting permission for admin01 to DECLINE")
      CfgPhysicalSwitch1 = CfgPhysicalSwitch()
      CfgPhysicalSwitch1.SetAttributes({"name": "G3_NEW", "type":CfgSwitchType.CFGLucentDefinityG3})
      CfgPhysicalSwitch1.Add()

      CfgSwitch1 = CfgSwitch()
      CfgSwitch1.SetAttributes({"name": SwitchName1, "physSwitchDBID":CfgPhysicalSwitch1 .DBID, "tenantDBID":CfgTenant1.DBID})
      CfgSwitch1.Add()
      CfgSwitch1.ChangeACE(cfgAdminID, "", isRecursive = 0) # DECLINE


      for userName in Persons1:
        cfgPerson = CfgPerson()
        cfgPerson .SetAttributes({"employeeID": userName, \
                                  "lastName": "LN" + userName, "firstName": "FN" + userName,\
                                  "tenantDBID":CfgTenant1.DBID, "userName": userName, \
                                  "emailAddress": userName + "@justmail.com"})
        cfgPerson.Add()
        CfgPersons1.append(cfgPerson)
        cfgPerson.ChangeACE(cfgAdminID, "", isRecursive = 0) # DECLINE
      
        
      for skill in Skills1:
        cfgSkill = CfgSkill()
        cfgSkill.SetAttributes({"name": skill, "tenantDBID":CfgTenant1.DBID})
        cfgSkill.Add()
        CfgSkills1.append(cfgSkill) 
        cfgSkill.ChangeACE(cfgAdminID, "", isRecursive = 0) # DECLINE
        
      raw_input("Start WFM_Config, login as 'admin01', password empty.\nMake SYNCHRONIZATION. ADD SWITCHES!!!\nDo not exit WFM_Config Config. Press Enter to continue")
      
      PrintLog("Checking tables after synchronization")
      
      cmd = "select * from WM_SITES where GSW_NAME = '%s'" %CfgSwitch1.name
      results = DBServer.ExecSQL(cmd, dbInfo)
      if len(results) != 0:
        WFM.testValue(description = "admin01 does not have access to switch %s, wrong results for %s:\n %s" %(CfgSwitch1.name, cmd, results), error = 1)
        cmd = "select * from WM_AGENTS"
        PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))    
      
      for cfgPerson in CfgPersons1:  
        cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID is NULL \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 0:
          WFM.testValue(description = "admin01 does not have access to person %s, wrong results for %s:\n %s" %(cfgPerson.employeeID, cmd, results), error = 1)
          cmd = "select * from WM_AGENTS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))    
 


      for cfgSkill in CfgSkills1:      
        cmd = "select * from WM_SKILLS where GSW_SKILL_ID = %d" %cfgSkill.DBID
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 0:
          WFM.testValue(description = "admin01 does not have access to skill %s, wrong results for %s:\n %s" %(cfgSkill.name, cmd, results), error = 1)     
          cmd = "select * from WM_SKILLS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      

      #
      PrintLog("Changing permissions for admin01 to FULL")
      
      CfgSwitch1.ChangeACE(cfgAdminID, "PEDXHCR", isRecursive = 0) # all
      
      for cfgPerson in CfgPersons1:
        cfgPerson.ChangeACE(cfgAdminID, "PEDXHCR", isRecursive = 0) # all

      for cfgSkill in CfgSkills1:
        cfgSkill.ChangeACE(cfgAdminID, "PEDXHCR", isRecursive = 0) # all
        
      raw_input("Make SYNCHRONIZATION on WFM_Config. ADD SWITCHES!!!. Press Enter to continue")

      cmd = "select * from WM_SITES where GSW_NAME = '%s'" %CfgSwitch1.name
      results = DBServer.ExecSQL(cmd, dbInfo)
      if len(results) != 1:
        WFM.testValue(description = "admin01 has FULL access to switch %s, wrong results for %s:\n %s" %(CfgSwitch1.name, cmd, results), error = 1)
        cmd = "select * from WM_AGENTS"
        PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))    
      
      for cfgPerson in CfgPersons1:  
        cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID is NULL \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "admin01 has FULL to person %s, wrong results for %s:\n %s" %(cfgPerson.employeeID, cmd, results), error = 1)
          cmd = "select * from WM_AGENTS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))    
 


      for cfgSkill in CfgSkills1:      
        cmd = "select * from WM_SKILLS where GSW_SKILL_ID = %d" %cfgSkill.DBID
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "admin01 has FULL access to skill %s, wrong results for %s:\n %s" %(cfgSkill.name, cmd, results), error = 1)     
          cmd = "select * from WM_SKILLS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))     
      
      
    finally:
      for obj in CfgPersons1:    
        if obj.exists:
          obj.Delete()  


      for obj in CfgSkills1:    
        if obj.exists:
          obj.Delete()      


      if CfgSwitch1 and CfgSwitch1.exists:
        CfgSwitch1.Delete()


      if CfgPhysicalSwitch1.exists:  
        CfgPhysicalSwitch1.Delete()       
    
  finally:
    CfgDAP.BeginChange()
    CfgDAP.userProperties["default"]["dbname"] = oldDBname
    CfgDAP.EndChange()     

    
    
      
def testDataAggregator_1():
  """Changing Agent Site assignment, DataAggregator test"""
  WFM = model.AbstractServer()
  WFM.appType = "WFM"

  dbInfo = CfgDAP.userProperties["default"]
  oldDBname = dbInfo["dbname"]  
  if not GetOption("DBName"):
    ProgrammError("Option DBName must be specified")  
  try:

    CfgDAP.BeginChange()
    CfgDAP.userProperties["default"]["dbname"] = GetOption("DBName")
    CfgDAP.EndChange()  
    dbInfo = CfgDAP.userProperties["default"]
    ScsWFMDataAggregator.Stop()    
    #--------------------------------------
    WMOnlyOneLogin = 1  
    WMUnassignAgents = 1
    #--------------------------------------
    TenantName = GetOption("TenantName")
    SwitchName1 = "PythonSwitch1"
    SwitchName2 = "PythonSwitch2"
    AgentLogins11 = []
    for i in range(0,3):
      AgentLogins11.append(str(1100+i))
    AgentLogins12 = []
    for i in range(0,3):
      AgentLogins12.append(str(2100+i))  
    AgentLogins21 = []
    for i in range(0,3):
      AgentLogins21.append(str(2200+i))       
    Persons1 = []
    for i in range(0,3):
      Persons1.append("P_1_2_" + str(100+i))    
    Persons2 = []
    for i in range(0,3):
      Persons2.append("P_2_" + str(200+i))       
    Skills1 = []
    for i in range(0,3):
      for j in range(1,3):
        Skills1.append("Skill" + str(i)+ str(j))    
    #--------------------------------------
    CfgAgentLogins11 = []  
    CfgAgentLogins12 = []  
    CfgAgentLogins21 = []  
    CfgSkills1 = []  
    CfgPersons1 = []  
    CfgPersons2 = []  
    CfgSwitch1 = None
    CfgSwitch2 = None
    #--------------------------------------
    CfgTenant1 = CfgTenant(TenantName)
    try:

      CfgPhysicalSwitch1 = CfgPhysicalSwitch()
      CfgPhysicalSwitch1.SetAttributes({"name": "G3_NEW", "type":CfgSwitchType.CFGLucentDefinityG3})
      CfgPhysicalSwitch1.Add()

      CfgSwitch1 = CfgSwitch()
      CfgSwitch1.SetAttributes({"name": SwitchName1, "physSwitchDBID":CfgPhysicalSwitch1 .DBID, "tenantDBID":CfgTenant1.DBID})
      CfgSwitch1.Add()

      CfgSwitch2 = CfgSwitch()
      CfgSwitch2.SetAttributes({"name": SwitchName2, "physSwitchDBID":CfgPhysicalSwitch1 .DBID, "tenantDBID":CfgTenant1.DBID})
      CfgSwitch2.Add()  
      PrintLog("Clear WM_SITES, WM_AGENTS, WM_SKILLS, WM_AGENT_SKILLS")
      cmd = "delete from WM_SITES"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_AGENTS"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_SKILLS"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_AGENT_SKILLS" 
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_TIMEZONES" 
      results = DBServer.ExecSQL(cmd, dbInfo)  
      cmd ="delete from WM_GLOBAL_SETTINGS" 
      results = DBServer.ExecSQL(cmd, dbInfo) 
      cfgServer = CfgSwitch1.cfgServer
      fltr = {"tenant_dbid": 1, "name": "PST"} # read PST from Environment 
      strTimeZone = cfgServer.GetObjectInfo(CfgObjectType.CFGTimeZone, fltr)[0]
      timeZoneDBID = cfgServer.GetObjectDBIDFromString(strTimeZone)
      timeZoneOffset = (float(cfgServer.GetObjectIntPropertyFromString(strTimeZone, "offset"))/2)*60
      cmd = "insert into WM_TIMEZONES (WM_NAME, WM_VALUE, GSW_TIMEZONE_ID, WM_ALLOW_SYNCH) values ('PST', %d, %d, 1)" %(timeZoneOffset, timeZoneDBID)
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd = "select WM_TIMEZONE_ID from WM_TIMEZONES where GSW_TIMEZONE_ID = %d" %timeZoneDBID
      results = DBServer.ExecSQL(cmd, dbInfo)
      tzID = int(results[0]['WM_TIMEZONE_ID'])
      PrintLog("Insert sites %s, %s into WM_SITES" %(CfgSwitch1.name, CfgSwitch2.name))
      cmd = "insert into WM_SITES (GSW_SWITCH_ID, WM_NAME, GSW_NAME, WM_TIMEZONE_ID, \
                                  WM_UNASSIGN_AGENTS, WM_ONLY_ONE_LOGIN) \
                                  values (%d, '%s', '%s', %d, %d, %d)" \
                                  %(CfgSwitch1.DBID, CfgSwitch1.name, CfgSwitch1.name, \
                                  tzID, WMUnassignAgents, WMOnlyOneLogin)
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd = "insert into WM_SITES (GSW_SWITCH_ID, WM_NAME, GSW_NAME, WM_TIMEZONE_ID, \
                                  WM_UNASSIGN_AGENTS, WM_ONLY_ONE_LOGIN) \
                                  values (%d, '%s', '%s', %d, %d, %d)" \
                                  %(CfgSwitch2.DBID, CfgSwitch2.name, CfgSwitch2.name, \
                                  tzID, WMUnassignAgents, WMOnlyOneLogin)
      results = DBServer.ExecSQL(cmd, dbInfo)  

      cmd = "insert into WM_SITES (WM_NAME,  WM_TIMEZONE_ID, \
                                  WM_UNASSIGN_AGENTS, WM_ONLY_ONE_LOGIN) \
                                  values ('%s', %d, %d, %d)" \
                                  %( "WM_CRAZY_SITE", tzID, WMUnassignAgents, \
                                  WMOnlyOneLogin)
      results = DBServer.ExecSQL(cmd, dbInfo)  


      cmd = "insert into WM_GLOBAL_SETTINGS (WM_ID, WM_EMAIL_SYNCH_ENABLED, WM_SCH_AUDIT_LEVEL, WM_CAL_AUDIT_LEVEL) values (1, 1, 0, 0)"
      results = DBServer.ExecSQL(cmd, dbInfo)  

      cmd = "select WM_SITE_ID from WM_SITES where GSW_SWITCH_ID = %d" %CfgSwitch1.DBID
      results = DBServer.ExecSQL(cmd, dbInfo)
      siteID1 = int(results[0]['WM_SITE_ID'])
      cmd = "select WM_SITE_ID from WM_SITES where GSW_SWITCH_ID = %d" %CfgSwitch2.DBID
      results = DBServer.ExecSQL(cmd, dbInfo)
      siteID2 = int(results[0]['WM_SITE_ID'])  
      PrintLog("Site ID for %s = %d, for %s = %d " %(CfgSwitch1.name, siteID1, CfgSwitch2.name, siteID2))

      PrintLog("Add %d Agent Logins for %s"%(len(AgentLogins11), CfgSwitch1.name))
      for loginCode in AgentLogins11:
        cfgAgentLogin = CfgAgentLogin()
        cfgAgentLogin.SetAttributes({"loginCode": loginCode, "tenantDBID":CfgTenant1.DBID, "switchDBID":CfgSwitch1.DBID})
        cfgAgentLogin.Add()  
        CfgAgentLogins11.append(cfgAgentLogin)

      PrintLog("Add %d Agent Logins for %s"%(len(AgentLogins12),CfgSwitch2.name))
      for loginCode in AgentLogins12:
        cfgAgentLogin = CfgAgentLogin()
        cfgAgentLogin.SetAttributes({"loginCode": loginCode, "tenantDBID":CfgTenant1.DBID, "switchDBID":CfgSwitch2.DBID})
        cfgAgentLogin.Add()  
        CfgAgentLogins12.append(cfgAgentLogin)   

      PrintLog("Add %d Agent Logins for %s"%(len(AgentLogins21),CfgSwitch2.name))
      for loginCode in AgentLogins21:
        cfgAgentLogin = CfgAgentLogin()
        cfgAgentLogin.SetAttributes({"loginCode": loginCode, "tenantDBID":CfgTenant1.DBID, "switchDBID":CfgSwitch2.DBID})
        cfgAgentLogin.Add()  
        CfgAgentLogins21.append(cfgAgentLogin)        

      PrintLog("Add %d skills"%len(Skills1))
      for skill in Skills1:
        cfgSkill = CfgSkill()
        cfgSkill.SetAttributes({"name": skill, "tenantDBID":CfgTenant1.DBID})
        cfgSkill.Add()
        CfgSkills1.append(cfgSkill)



      ind1 = 0  
      PrintLog("Add %d Agents with 1 login to %s and 1 login to %s and 2 skills"%(len(Persons1), CfgSwitch1.name, CfgSwitch2.name))
      PrintLog("These agents should created as unassigned in WMF")
      for userName in Persons1:
        cfgPerson = CfgPerson()
        agentLoginInfo1 = CfgAgentLoginInfo()
        agentLoginInfo1.agentLoginDBID = CfgAgentLogins11[ind1/2].DBID
        agentLoginInfo2 = CfgAgentLoginInfo()
        agentLoginInfo2.agentLoginDBID = CfgAgentLogins12[ind1/2].DBID
        skillLevel1 = CfgSkillLevel()
        skillLevel1.skillDBID = CfgSkills1[ind1].DBID
        skillLevel2 = CfgSkillLevel()
        skillLevel2.skillDBID = CfgSkills1[ind1+1].DBID 
        agentInfo = CfgAgentInfo()
        agentInfo.agentLogins = [agentLoginInfo1, agentLoginInfo2]
        agentInfo.skillLevels = [skillLevel1, skillLevel2]
        cfgPerson .SetAttributes({"agentInfo": agentInfo, "employeeID": userName, \
                                  "lastName": "LN" + userName, "firstName": "FN" + userName,\
                                  "tenantDBID":CfgTenant1.DBID, "userName": userName})
        cfgPerson.Add()
        CfgPersons1.append(cfgPerson)
        ind1 = ind1 + 2  

      ind1 = 0  
      PrintLog("Add %d Agents with 1 login to %s and 2 skills"%(len(Persons2), CfgSwitch2.name))
      PrintLog("These agents should created as assigned to %s site in WMF" %CfgSwitch2.name)
      for userName in Persons2:
        cfgPerson = CfgPerson()
        agentLoginInfo2 = CfgAgentLoginInfo()
        agentLoginInfo2.agentLoginDBID = CfgAgentLogins21[(ind1)/2].DBID
        skillLevel1 = CfgSkillLevel()
        skillLevel1.skillDBID = CfgSkills1[ind1].DBID
        skillLevel2 = CfgSkillLevel()
        skillLevel2.skillDBID = CfgSkills1[ind1+1].DBID 
        agentInfo = CfgAgentInfo()
        agentInfo.agentLogins = [agentLoginInfo2]
        agentInfo.skillLevels = [skillLevel1, skillLevel2]
        cfgPerson .SetAttributes({"agentInfo": agentInfo, "employeeID": userName, "lastName": \
                                  "LN" + userName, "firstName": "FN" + userName,\
                                  "tenantDBID":CfgTenant1.DBID, "userName": userName})
        cfgPerson.Add()
        CfgPersons2.append(cfgPerson)
        ind1 = ind1 + 2  
      #-----------------------------------------------
      #Start Data Aggregator
      ScsWFMDataAggregator.Start() 
      time.sleep(15)
      #-----------------------------------------------
      cmd = "select WM_EMAIL_SYNCH_ENABLED from WM_GLOBAL_SETTINGS"
      results = DBServer.ExecSQL(cmd, dbInfo)
      emailSync = int(results[0]["WM_EMAIL_SYNCH_ENABLED"])  

      PrintLog("Checking tables after synchronization")
      for cfgSkill in CfgSkills1:
        cmd = "select * from WM_SKILLS where GSW_SKILL_ID = %d and WM_NAME = '%s'" %(cfgSkill.DBID, cfgSkill.name)
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "New skill added, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_SKILLS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))    

      for cfgPerson in CfgPersons1:
        #unassigned agents should be added
        if emailSync and cfgPerson.emailAddress:
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID is NULL \
                and GSW_EMAIL = '%s' \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName, cfgPerson.emailAddress)
        else:               
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID is NULL \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName)


        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))
        else:  
          cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d and  GSW_SKILL_ID = %d and GSW_SKILL_LEVEL = %d" \
                 %(cfgPerson.DBID, cfgPerson.agentInfo.skillLevels[0].skillDBID, cfgPerson.agentInfo.skillLevels[0].level ) 
          time.sleep(1)
          results = DBServer.ExecSQL(cmd, dbInfo)
          if len(results) != 1:
            WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)  
            cmd = "select * from WM_AGENT_SKILLS"
            PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))

          cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d and  GSW_SKILL_ID = %d and GSW_SKILL_LEVEL = %d" \
                 %(cfgPerson.DBID, cfgPerson.agentInfo.skillLevels[1].skillDBID, cfgPerson.agentInfo.skillLevels[1].level ) 
          time.sleep(1)
          results = DBServer.ExecSQL(cmd, dbInfo)
          if len(results) != 1:
            WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)
            cmd = "select * from WM_AGENT_SKILLS"
            PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))




      for cfgPerson in CfgPersons2:  
        #assigned agents should be added
        if emailSync and cfgPerson.emailAddress:
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d \
                and GSW_EMAIL = '%s' \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName, siteID2, cfgPerson.emailAddress)
        else:               
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName, siteID2)

        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))
        else:
          cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d and  GSW_SKILL_ID = %d and GSW_SKILL_LEVEL = %d" \
                 %(cfgPerson.DBID, cfgPerson.agentInfo.skillLevels[0].skillDBID, cfgPerson.agentInfo.skillLevels[0].level ) 
          time.sleep(1)
          results = DBServer.ExecSQL(cmd, dbInfo)
          if len(results) != 1:
            WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)  
            cmd = "select * from WM_AGENT_SKILLS"
            PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))

          cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d and  GSW_SKILL_ID = %d and GSW_SKILL_LEVEL = %d" \
                 %(cfgPerson.DBID, cfgPerson.agentInfo.skillLevels[1].skillDBID, cfgPerson.agentInfo.skillLevels[1].level ) 
          time.sleep(1)
          results = DBServer.ExecSQL(cmd, dbInfo)
          if len(results) != 1:
            WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)
            cmd = "select * from WM_AGENT_SKILLS"
            PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))



      #-----------------------------------------------      
      #Delete agent logins from CfgAgentLogins12
      PrintLog("Delete Agent Logins for Site2 belonging to persons with 2 agent logins. Persons should be assigned to Site1")
      for cfgAgentLogin in CfgAgentLogins12:
        cfgAgentLogin.Delete()
        #agents should be assigned to site1  

      #raw_input("To check WFM_Config, make SYNCHRONIZATION on WFM_Config. To check DataAggregator just press enter")

      for cfgPerson in CfgPersons1:
        cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and WM_SITE_ID = %d" %(cfgPerson.DBID, siteID1)
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "Agent should be assigned to site, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS where GSW_AGENT_ID = %d" % cfgPerson.DBID
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      


      #-----------------------------------------------      
      #Delete agent logins from CfgAgentLogins11
      PrintLog("Delete Agent Logins for Site1. Persons from Site1 should go unassigned")
      ind = 0
      for cfgAgentLogin in CfgAgentLogins11:
        cfgAgentLogin.Delete()
        cfgPerson = CfgPersons1[ind]
        ind = ind + 1

        #Agents form site1 should go unissigned
        cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and WM_SITE_ID is NULL"%cfgPerson.DBID
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "Agent login, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS where GSW_AGENT_ID = %d "%cfgPerson.DBID
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      

      #-----------------------------------------------
      #Delete person
      PrintLog("Delete Persons assigned to Site2. WM_END_DATE should be set to current")
      for cfgPerson in CfgPersons2:
        cfgPerson.Delete()
        cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and WM_END_DATE = '%s'" \
               %(cfgPerson.DBID, time.strftime("%Y-%m-%d 00:00:00.000", time.localtime()))             
  #             %(cfgPerson.DBID, time.strftime("%Y-%m-%d 00:00:00.000", time.gmtime()))
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "Person deleted, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS where GSW_AGENT_ID = %d"%cfgPerson.DBID
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      


    finally:
      for obj in CfgPersons1:    
        if obj.exists:
          obj.Delete()

      for obj in CfgPersons2:    
        if obj.exists:
          obj.Delete()      

      for obj in CfgSkills1:    
        if obj.exists:
          obj.Delete()      

      for obj in CfgAgentLogins11:    
        if obj.exists:
          obj.Delete() 

      for obj in CfgAgentLogins12:    
        if obj.exists:
          obj.Delete()  
      for obj in CfgAgentLogins21:    
        if obj.exists:
          obj.Delete()  
          

      if CfgSwitch1 and CfgSwitch1.exists:
        CfgSwitch1.Delete()
      if CfgSwitch2 and CfgSwitch2.exists:
        CfgSwitch2.Delete()

      if CfgPhysicalSwitch1.exists:  
        CfgPhysicalSwitch1.Delete()   
  finally:
    CfgDAP.BeginChange()
    CfgDAP.userProperties["default"]["dbname"] = oldDBname
    CfgDAP.EndChange() 
    ScsWFMDataAggregator.Stop()  
    
      
def testDataAggregator_2():
  WFM = model.AbstractServer()
  WFM.appType = "WFM"
  dbInfo = CfgDAP.userProperties["default"]
  oldDBname = dbInfo["dbname"]  
  if not GetOption("DBName"):
    ProgrammError("Option DBName must be specified")  
  try:

    CfgDAP.BeginChange()
    CfgDAP.userProperties["default"]["dbname"] = GetOption("DBName")
    CfgDAP.EndChange()  
    dbInfo = CfgDAP.userProperties["default"]
    ScsWFMDataAggregator.Stop()  
    #--------------------------------------
    WMOnlyOneLogin = 1  
    WMUnassignAgents = 1
    #--------------------------------------
    TenantName = GetOption("TenantName")
    SwitchName1 = "PythonSwitch1"
    AgentLogins1 = []
    for i in range(0,3):
      AgentLogins1.append(str(1100+i))
    Persons1 = []
    for i in range(0,3):
      Persons1.append("P_1_2_" + str(100+i))    
    Skills1 = []
    for i in range(0,3):
      for j in range(1,3):
        Skills1.append("Skill" + str(i)+ str(j))    
    #--------------------------------------
    CfgAgentLogins1 = []  
    CfgSkills1 = []  
    CfgPersons1 = []  
    CfgSwitch1 = None
    #--------------------------------------
    CfgTenant1 = CfgTenant(TenantName)
    try:

      CfgPhysicalSwitch1 = CfgPhysicalSwitch()
      CfgPhysicalSwitch1.SetAttributes({"name": "G3_NEW", "type":CfgSwitchType.CFGLucentDefinityG3})
      CfgPhysicalSwitch1.Add()

      CfgSwitch1 = CfgSwitch()
      CfgSwitch1.SetAttributes({"name": SwitchName1, "physSwitchDBID":CfgPhysicalSwitch1 .DBID, "tenantDBID":CfgTenant1.DBID})
      CfgSwitch1.Add()

      PrintLog("Clear WM_SITES, WM_AGENTS, WM_SKILLS, WM_AGENT_SKILLS")
      cmd = "delete from WM_SITES"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_AGENTS"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_SKILLS"
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_AGENT_SKILLS" 
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd ="delete from WM_TIMEZONES" 
      results = DBServer.ExecSQL(cmd, dbInfo)  
      cmd ="delete from WM_GLOBAL_SETTINGS" 
      results = DBServer.ExecSQL(cmd, dbInfo) 
      cfgServer = CfgSwitch1.cfgServer
      fltr = {"tenant_dbid": 1, "name": "PST"} # read PST from Environment 
      strTimeZone = cfgServer.GetObjectInfo(CfgObjectType.CFGTimeZone, fltr)[0]
      timeZoneDBID = cfgServer.GetObjectDBIDFromString(strTimeZone)
      timeZoneOffset = (float(cfgServer.GetObjectIntPropertyFromString(strTimeZone, "offset"))/2)*60
      cmd = "insert into WM_TIMEZONES (WM_NAME, WM_VALUE, GSW_TIMEZONE_ID, WM_ALLOW_SYNCH) values ('PST', %d, %d, 1)" %(timeZoneOffset, timeZoneDBID)
      results = DBServer.ExecSQL(cmd, dbInfo)
      cmd = "select WM_TIMEZONE_ID from WM_TIMEZONES where GSW_TIMEZONE_ID = %d" %timeZoneDBID
      results = DBServer.ExecSQL(cmd, dbInfo)
      tzID = int(results[0]['WM_TIMEZONE_ID'])
      PrintLog("Insert site %s, into WM_SITES" %(CfgSwitch1.name))
      cmd = "insert into WM_SITES (GSW_SWITCH_ID, WM_NAME, GSW_NAME, WM_TIMEZONE_ID, \
                                  WM_UNASSIGN_AGENTS, WM_ONLY_ONE_LOGIN) \
                                  values (%d, '%s', '%s', %d, %d, %d)" \
                                  %(CfgSwitch1.DBID, CfgSwitch1.name, CfgSwitch1.name, \
                                  tzID, WMUnassignAgents, WMOnlyOneLogin)
      results = DBServer.ExecSQL(cmd, dbInfo)

      cmd = "insert into WM_SITES (WM_NAME,  WM_TIMEZONE_ID, \
                                  WM_UNASSIGN_AGENTS, WM_ONLY_ONE_LOGIN) \
                                  values ('%s', %d, %d, %d)" \
                                  %( "WM_CRAZY_SITE", tzID, WMUnassignAgents, \
                                  WMOnlyOneLogin)
      results = DBServer.ExecSQL(cmd, dbInfo)  

      cmd = "insert into WM_GLOBAL_SETTINGS (WM_ID, WM_EMAIL_SYNCH_ENABLED, WM_SCH_AUDIT_LEVEL, WM_CAL_AUDIT_LEVEL) values (1, 1, 0, 0)"
      results = DBServer.ExecSQL(cmd, dbInfo)  


      cmd = "select WM_SITE_ID from WM_SITES where GSW_SWITCH_ID = %d" %CfgSwitch1.DBID
      results = DBServer.ExecSQL(cmd, dbInfo)
      siteID1 = int(results[0]['WM_SITE_ID'])
      PrintLog("Site ID for %s = %d" %(CfgSwitch1.name, siteID1))

      PrintLog("Add %d Agent Logins for %s"%(len(AgentLogins1), CfgSwitch1.name))
      for loginCode in AgentLogins1:
        cfgAgentLogin = CfgAgentLogin()
        cfgAgentLogin.SetAttributes({"loginCode": loginCode, "tenantDBID":CfgTenant1.DBID, "switchDBID":CfgSwitch1.DBID})
        cfgAgentLogin.Add()  
        CfgAgentLogins1.append(cfgAgentLogin)


      PrintLog("Add %d skills"%len(Skills1))
      for skill in Skills1:
        cfgSkill = CfgSkill()
        cfgSkill.SetAttributes({"name": skill, "tenantDBID":CfgTenant1.DBID})
        cfgSkill.Add()
        CfgSkills1.append(cfgSkill)



      ind1 = 0  
      PrintLog("Add %d Agents with 1 login to %s and 2 skills"%(len(Persons1), CfgSwitch1.name))
      PrintLog("These agents should created as assigned to Site1 WMF")
      for userName in Persons1:
        cfgPerson = CfgPerson()
        agentLoginInfo1 = CfgAgentLoginInfo()
        agentLoginInfo1.agentLoginDBID = CfgAgentLogins1[ind1/2].DBID

        skillLevel1 = CfgSkillLevel()
        skillLevel1.skillDBID = CfgSkills1[ind1].DBID
        skillLevel2 = CfgSkillLevel()
        skillLevel2.skillDBID = CfgSkills1[ind1+1].DBID 
        agentInfo = CfgAgentInfo()
        agentInfo.agentLogins = [agentLoginInfo1]
        agentInfo.skillLevels = [skillLevel1, skillLevel2]
        cfgPerson .SetAttributes({"agentInfo": agentInfo, "employeeID": userName, \
                                  "lastName": "LN" + userName, "firstName": "FN" + userName,\
                                  "tenantDBID":CfgTenant1.DBID, "userName": userName})
        cfgPerson.Add()
        CfgPersons1.append(cfgPerson)
        ind1 = ind1 + 2  

      ind1 = 0  
      #-----------------------------------------------
      #Start Data Aggregator
      ScsWFMDataAggregator.Start() 
      time.sleep(15)
      #-----------------------------------------------      
      cmd = "select WM_EMAIL_SYNCH_ENABLED from WM_GLOBAL_SETTINGS"
      results = DBServer.ExecSQL(cmd, dbInfo)
      emailSync = int(results[0]["WM_EMAIL_SYNCH_ENABLED"])  

      PrintLog("Checking tables after synchronization")
      for cfgSkill in CfgSkills1:
        cmd = "select * from WM_SKILLS where GSW_SKILL_ID = %d and WM_NAME = '%s'" %(cfgSkill.DBID, cfgSkill.name)
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "New skill added, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_SKILLS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))    



      for cfgPerson in CfgPersons1:  
        #assigned agents should be added
        if emailSync and cfgPerson.emailAddress:
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d \
                and GSW_EMAIL = '%s' \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName, siteID1, cfgPerson.emailAddress)
        else:               
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName, siteID1)


        #cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
        #      and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d and WM_END_DATE is NULL" \
        #       %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName,  siteID1)
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))
        else:
          cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d and  GSW_SKILL_ID = %d and GSW_SKILL_LEVEL = %d" \
                 %(cfgPerson.DBID, cfgPerson.agentInfo.skillLevels[0].skillDBID, cfgPerson.agentInfo.skillLevels[0].level ) 
          time.sleep(1)
          results = DBServer.ExecSQL(cmd, dbInfo)
          if len(results) != 1:
            WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)  
            cmd = "select * from WM_AGENT_SKILLS"
            PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))

          cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d and  GSW_SKILL_ID = %d and GSW_SKILL_LEVEL = %d" \
                 %(cfgPerson.DBID, cfgPerson.agentInfo.skillLevels[1].skillDBID, cfgPerson.agentInfo.skillLevels[1].level ) 
          time.sleep(1)
          results = DBServer.ExecSQL(cmd, dbInfo)
          if len(results) != 1:
            WFM.testValue(description = "New person added, wrong results for %s:\n %s" %(cmd, results), error = 1)
            cmd = "select * from WM_AGENT_SKILLS"
            PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))


      #Change Switch Name
      PrintLog("Change switch name from %s to PythonSwitch1NameChanged" %CfgSwitch1.name )
      CfgSwitch1.BeginChange()
      newName = "PythonSwitch1NameChanged"
      CfgSwitch1.name = newName
      CfgSwitch1.EndChange()
      cmd = "select * from WM_SITES where GSW_SWITCH_ID = %d and GSW_NAME = '%s'" %(CfgSwitch1.DBID, newName)
      time.sleep(1)
      results = DBServer.ExecSQL(cmd, dbInfo)
      if len(results) != 1:
        WFM.testValue(description = "Switch name changed, wrong results for %s:\n %s" %(cmd, results), error = 1)     
        cmd = "select * from WM_SITES where GSW_SWITCH_ID = %d"%CfgSwitch1.DBID
        PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))

      #-----------------------------------------------      


      for cfgPerson in CfgPersons1:
        cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and WM_SITE_ID = %d" %(cfgPerson.DBID, siteID1)
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "Agent should be assigned to site, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS where GSW_AGENT_ID = %d" % cfgPerson.DBID
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      


      #-----------------------------------------------
      #Delete one Skill from agent
      PrintLog("Delete one Skill shortcut from persons" )    
      for cfgPerson in CfgPersons1:
        cfgPerson.BeginChange()
        removedSkillDBID = cfgPerson.agentInfo.skillLevels[0].skillDBID
        del cfgPerson.agentInfo.skillLevels[0]
        cfgPerson.EndChange()
        cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d and GSW_SKILL_ID = %d"  %(cfgPerson.DBID, removedSkillDBID)  
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 0:
          WFM.testValue(description = "Skill removed from person, wrong results for %s:\n %s" %(cmd, results), error = 1)     
          cmd = "select * from WM_AGENT_SKILLS where GSW_AGENT_ID = %d" %cfgPerson.DBID
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      

      #-----------------------------------------------
      #Change skill name
      PrintLog("Cbange Skill names" )    
      for cfgSkill in CfgSkills1:
        cfgSkill.BeginChange()
        cfgSkill.name = cfgSkill.name + "Changed"
        cfgSkill.EndChange()
        cmd = "select * from WM_SKILLS where GSW_SKILL_ID = %d and WM_NAME = '%s'" %(cfgSkill.DBID, cfgSkill.name)
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "Skill name changed, wrong results for %s:\n %s" %(cmd, results), error = 1)     
          cmd = "select * from WM_SKILLS where GSW_SKILL_ID = %d" %cfgSkill.DBID
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      

      #-----------------------------------------------
      #Delete the rest of skills 
      PrintLog("Delete  the rest of Skills" )    
      for cfgSkill in CfgSkills1:
        cfgSkill.Delete()
        cmd = "select * from WM_SKILLS where GSW_SKILL_ID = %d" %cfgSkill.DBID
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 0:
          WFM.testValue(description = "Skill deleted, wrong results for %s:\n %s" %(cmd, results), error = 1)     
          cmd = "select * from WM_SKILLS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      

        cmd = "select * from WM_AGENT_SKILLS where GSW_SKILL_ID = %d"  %cfgSkill.DBID 
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 0:
          WFM.testValue(description = "Skill deleted, wrong results for %s:\n %s" %(cmd, results), error = 1)     
          cmd = "select * from WM_SKILLS"
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      


      #-----------------------------------------------
      #Change persons lastname, first name
      PrintLog("Change  persons lastname, first name" )    
      for cfgPerson in CfgPersons1:
        cfgPerson.BeginChange()
        cfgPerson.lastName = cfgPerson.lastName + "Changed"
        cfgPerson.firstName = cfgPerson.firstName + "Changed"
        cfgPerson.EndChange()
        if emailSync and cfgPerson.emailAddress:
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d \
                and GSW_EMAIL = '%s' \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName, siteID1, cfgPerson.emailAddress)
        else:               
          cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
                and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d \
                and WM_END_DATE is NULL" \
                 %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName, siteID1)

        #cmd = "select * from WM_AGENTS  where GSW_AGENT_ID = %d and GSW_EMPLOYEE_ID = '%s' \
        #      and  GSW_FIRST_NAME = '%s' and GSW_LAST_NAME = '%s' and  WM_SITE_ID = %d and WM_END_DATE is NULL" \
        #       %(cfgPerson.DBID, cfgPerson.employeeID, cfgPerson.firstName, cfgPerson.lastName,  siteID1)
        time.sleep(1)
        results = DBServer.ExecSQL(cmd, dbInfo)
        if len(results) != 1:
          WFM.testValue(description = "Person changed, wrong results for %s:\n %s" %(cmd, results), error = 1)
          cmd = "select * from WM_AGENTS where GSW_AGENT_ID = %d"%cfgPerson.DBID
          PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      


      #-----------------------------------------------
      #Last Delete Switch
      PrintLog("Delete Switch related to Site1. GSW_NAME and GSW_SWITCH_ID should be set to NULL")
      CfgSwitch1.Delete()
      cmd = "select * from WM_SITES where WM_SITE_ID = %d and GSW_NAME = '' and GSW_SWITCH_ID is NULL" %siteID1
      time.sleep(1)
      results = DBServer.ExecSQL(cmd, dbInfo)
      if len(results) != 1:
        WFM.testValue(description = "Switch deleted, wrong results for %s:\n %s" %(cmd, results), error = 1)       
        cmd = "select * from WM_SITES"
        PrintLog("Results of '%s':\n%s"%(cmd, DBServer.ExecSQL(cmd, dbInfo)))      

    finally:
      for obj in CfgPersons1:    
        if obj.exists:
          obj.Delete()  


      for obj in CfgSkills1:    
        if obj.exists:
          obj.Delete()      

      for obj in CfgAgentLogins1:    
        if obj.exists:
          obj.Delete() 


      if CfgSwitch1 and CfgSwitch1.exists:
        CfgSwitch1.Delete()


      if CfgPhysicalSwitch1.exists:  
        CfgPhysicalSwitch1.Delete()   
  finally:
    CfgDAP.BeginChange()
    CfgDAP.userProperties["default"]["dbname"] = oldDBname
    CfgDAP.EndChange()     
    ScsWFMDataAggregator.Stop()  
    
    